using System;
using System.Collections.Generic;
using FishFillets.Model;
using FishFillets.ObjectLogic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using FishFillets.GameComponents.Helpers;

namespace FishFillets.GameComponents
{
    /// <summary>
    /// Game component representing single level.
    /// </summary>
    public class PanelComponent : DrawableGameComponent
    {
        private readonly ContentManager contentManager;
        private readonly AssetHelper contentAltManager;
        private readonly GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        private Texture2D panel;
        private Texture2D panelClicked;
        private int x0;
        private Dictionary<PanelButton, double> pressedCounter;
        private const int pressedDelta = 250;

        public PanelComponent(Game game, GraphicsDeviceManager graphics) 
            : base(game)
        {
            this.graphics = graphics;
            contentManager = new ContentManager(game.Services) { RootDirectory = "Content" };
            contentAltManager = new AssetHelper(Game.GraphicsDevice, contentManager.RootDirectory);
        }

        public override void Initialize()
        {
            pressedCounter = new Dictionary<PanelButton, double>
            {
                { PanelButton.SmallUp, 0 },
                { PanelButton.SmallLeft, 0 },
                { PanelButton.SmallDown, 0 },
                { PanelButton.SmallRight, 0 },
                { PanelButton.Switch, 0 },
                { PanelButton.Abort, 0 },
                { PanelButton.Restart, 0 },
            };

            base.Initialize();
        }

        protected override void LoadContent()
        {
            panel = contentAltManager.LoadTextureStream("images/menu/o3");
            panelClicked = contentAltManager.LoadTextureStream("images/menu/o4");
            x0 = graphics.PreferredBackBufferWidth - panel.Width;
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
            contentAltManager.UnloadAll();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            // background
            spriteBatch.Draw(panel, new Rectangle(x0, 0, panel.Width, panel.Height), Color.White);

            // up
            if (pressedCounter[PanelButton.SmallUp] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0 + 50, 0, 50, 50), new Rectangle(50, 0, 50, 50), Color.White);
            // left
            if (pressedCounter[PanelButton.SmallLeft] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0, 50, 50, 50), new Rectangle(0, 50, 50, 50), Color.White);
            // right
            if (pressedCounter[PanelButton.SmallRight] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0 + 100, 50, 50, 50), new Rectangle(100, 50, 50, 50), Color.White);
            // down
            if (pressedCounter[PanelButton.SmallDown] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0 + 50, 100, 50, 50), new Rectangle(50, 100, 50, 50), Color.White);

            // switch
            if (pressedCounter[PanelButton.Switch] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0 + 25, 150, 110, 25), new Rectangle(25, 150, 110, 25), Color.White);

            // abort
            if (pressedCounter[PanelButton.Abort] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0, 354, 105, 17), new Rectangle(0, 354, 105, 17), Color.White);
            // restart
            if (pressedCounter[PanelButton.Restart] >= gameTime.TotalGameTime.TotalMilliseconds)
                spriteBatch.Draw(panelClicked, new Rectangle(x0, 371, 110, 24), new Rectangle(0, 371, 110, 24), Color.White);

            spriteBatch.End();
        }

        public void TryClickButton(int x, int y, GameTime gameTime)
        {
            // up
            if (x >= x0 + 50 && x < x0 + 100 && y >= 0 && y < 50)
            {
                pressedCounter[PanelButton.SmallUp] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.Move(Direction.Up);
            }
            // left
            else if (x >= x0 && x < x0 + 50 && y >= 50 && y < 100)
            {
                pressedCounter[PanelButton.SmallLeft] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.Move(Direction.Left);
            }
            // right
            else if (x >= x0 + 100 && x < x0 + 150 && y >= 50 && y < 100)
            {
                pressedCounter[PanelButton.SmallRight] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.Move(Direction.Right);
            }
            // down
            else if (x >= x0 + 50 && x < x0 + 100 && y >= 100 && y < 150)
            {
                pressedCounter[PanelButton.SmallDown] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.Move(Direction.Down);
            }
            // switch
            else if (x >= x0 + 25 && x < x0 + 135 && y >= 150 && y < 175)
            {
                pressedCounter[PanelButton.Switch] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.SwitchPlayer();
            }
            // abort
            else if (x >= x0 && x < x0 + 105 && y >= 354 && y < 371)
            {
                pressedCounter[PanelButton.Abort] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.AbortLevel();
            }
            // restart
            else if (x >= x0 && x < x0 + 110 && y >= 371 && y < 395)
            {
                pressedCounter[PanelButton.Restart] = gameTime.TotalGameTime.TotalMilliseconds + pressedDelta;
                GameController.Instance.RestartLevel();
            }
        }
    }

    public enum PanelButton
    {
        SmallUp = 0,
        SmallLeft = 1,
        SmallRight = 2,
        SmallDown = 3,

        BigUp = 4,
        BigLeft = 5,
        BigRight = 6,
        BigDown = 7,

        Switch = 8,

        Save = 9,
        Load = 10,
        Abort = 11,
        Restart = 12,

        Options = 13,
    }
}
